<template>
  <ExamPaper :items="paper" @update:answers="(val) => (finalAnswers = val)" />
</template>

<script setup>
import { ref, onMounted } from "vue";
import ExamPaper from "@/components/ExamPaper.vue";

const paper = ref([
  {
    question: `<img src="">`,
    answer: `
    function deal1(str){
      const regNumber = /(\d)/;
      const regLower = /([a-z])/;
      const regUpper = /([A-Z])/;
      let statusCache = 0;
      let strCache = '';
      const arrStore = [];
      const setList = new Set();
      // 中断本次（跳过当前这一轮，继续下一轮）	continue	立即进入下一次循环
      // 中断所有（整个循环结束）	break	跳出整个循环体，后面的循环也不再执行
      for(let i=0;i<str.length;i++){
        let status = 1;
        const code = str[i];
        if(regNumber.test(code)){
          status = 2;
        }else if(regLower.test(code)){
          status = 3;
        }else if(regUpper.test(code)){
          status = 4;
        }else{
          status = 1;
        }
        
        if(!statusCache){
          strCache += code;
          statusCache = status;
        }else{
          if(status === statusCache){
            strCache += code;
          }else{
            arrStore.push(strCache);
            strCache = code;
            statusCache = status;
          }
        }
        if(i === str.length-1){
          arrStore.push(strCache);
        }
        setList.add(status);
      }
      if((new Set(arrStore).size !== arrStore.length) || (setList.size<3) || str.length<=8){
        console.log('NG');
      }else{
        console.log('OK');
      }
    }
    deal1('021Abc9000')
    deal1('021Abc9Abc1')
    deal1('021ABC9000')
    deal1('021$bc9000') 
    `,
  },
  {
    question: `<img src="">
    <img src="">`,
    answer: `
    function deal2(number){
      //转换为十进制parseInt("1111", 2);
      //转换为二进制
      const binary = number.toString(2);
      let length = 0;
      binary.split('0').forEach(item=>{
        if(item.length>length){
          length = item.length;
        }
      })
      console.log(length)
      return length
    }
    deal2(200)
    `,
  },
  {
    question: `<img src="">
    <img src="">`,
    answer: `
    function deal3(row,col,mapArr){
      //判断是否到达边界
      function check(x,y){
        if(x<0 || x>row-1 || y<0 || y>col-1){
          return true;
        }
        return false;
      }
      const finalAnswers = [];
      function toStep(position=[0,0],stepList=[],stepNum = 1,sourceDirection='init', parent){
        const [x,y] = position;
        //判断是否到达边界
        if(check(x,y)){
          return;
        }
        //如果是墙走不过
        if(mapArr[x][y] === 1){
          return;
        }
        
        stepList.push(position);
        const stepData = {
          position,
          stepNum:stepNum,
          stepList,
          sourceDirection,
          parent
        }
        //判断是否到达终点
        if(x === row-1 && y === col-1){
          finalAnswers.push(stepData)
          return;
        }
        //上
        sourceDirection!=='down' && toStep([x-1,y],[...stepList],stepNum+1,'up',stepData);
        //下
        sourceDirection!=='up' && toStep([x+1,y],[...stepList],stepNum+1,'down',stepData);
        //左
        sourceDirection!=='right' && toStep([x,y-1],[...stepList],stepNum+1,'left',stepData);
        //右
        sourceDirection!=='left' && toStep([x,y+1],[...stepList],stepNum+1,'right',stepData);
      }
      toStep()
      if(finalAnswers.length){
        //根据步数从小到大排序
        finalAnswers.sort((a,b)=>{
          return a.stepNum - b.stepNum
        })
        //输出步数最少的那个结果的路径
        finalAnswers[0].stepList.forEach(item=>{
          console.log(\`(\${item[0]},\${item[1]})\`);
        })
      }
      return finalAnswers
    }
    deal3(5,5,[
      [0,1,0,0,0],
      [0,1,1,1,0],
      [0,0,0,0,0],
      [0,1,1,1,0],
      [0,0,0,1,0]
    ])
    // 复习时做法
    function deal3(rows, cols, grid) {
      const maxRow = rows;
      const maxCol = cols;
      const visted = new Set();
      let result;
      function step(row, col, path=[]) {
        if(result){
          return
        }
        if (
          row < 0 ||
          col < 0 ||
          row >= maxRow ||
          col >= maxCol ||
          visted.has(\`\${row}-\${col}\`)
        ) {
          return
        }else if(grid[row][col] === 1){
          return
        }else{
          path.push([row,col]);
          visted.add(\`\${row}-\${col}\`);
          if(row==maxRow-1 && col==maxCol-1){
            result = path;
            return
          }
          step(row+1,col,path);
          step(row,col+1,path);
          step(row-1,col,path);
          step(row,col-1,path);
          if(result){
            return
          }
          path.pop();
          visted.delete(\`\${row}-\${col}\`);
        }
      }
      step(0,0);
      console.log(result)
      return result
    }
    `,
  },
]);

onMounted(() => {

});

const finalAnswers = ref([]); // 实时收集到的答案
</script>
